<!DOCTYPE html>
<html>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
<meta name=referrer content=never>
<title>中秋节贺卡</title>
<script src="__STATIC__/doclist/js/jquery-min.js"></script>
<script src="__STATIC__/doclist/js/jquery.qrcode.min.js"></script>
<style type="text/css">

    body,html{
        margin:0;padding:0;
    }
</style>
</head>
<body>
<!--<a href="{:url('index/red/share',['id'=>'id'])}">分享</a>-->
<!--<h1>查看更多</h1>-->
<!--<a href="{:url('index/game/index')}">查看七夕节应该会发生的五件事</a>-->

<img id="showImgs" src="" style="width: 100%;height: auto;">

<div style="text-align: center;display: none" id="qrPic"></div>
<script type="text/javascript">
    var showImg = document.getElementById("showImgs");
    var name = '{$username}';//$username
    var mywish = '{$mywish}';
    var descmsg = '{$ins_msg}';//$二维码链接
    var bgimg = '{$path}';//$path
    setImgText({
        name:name+'   (长按保存图片分享) ',
        bgimg:bgimg,
        descmsg:mywish,
        link:descmsg,
        success:function(url){
            showImg.src = url;
        }
    });
    function setImgText(data) {
        console.log(data);
        var c = document.createElement('canvas');
        // var ewmimg = 'https://api.qrserver.com/v1/create-qr-code/?size=150x150&data='+data.link

        var img = new Image();
        img.setAttribute("crossOrigin", 'Anonymous');
        img.onload = function () {
            c.width = img.width;
            c.height = img.height;
            var cxt = c.getContext("2d");
            cxt.drawImage(img, 0, 0, c.width, c.height);
            cxt.save();
            cxt.font = "30px Arial";
            cxt.textBaseline = 'middle';
            cxt.fillStyle = '#4e4e4e';
            cxt.textAlign = 'left';
            cxt.fillText(data.name, 85, 1050);

            cxt.fillStyle = '#ff0000';
            var result = breakLinesForCanvas(data.descmsg, 690, '30px 微软雅黑');
            result.forEach(function(item,index){
                var aa = index * 35;
                cxt.fillText(item, 30, 1100 + aa);
            });

            var ewm = new Image();
            

            var ewmimg = qr(data.link);

            ewm.onload = function(){
                cxt.drawImage(ewm, 595, 1140, 130, 130);
                cxt.save();
                var tempSrc = c.toDataURL("image/png");
                data.success(tempSrc);
            }
            ewm.setAttribute("crossOrigin", 'Anonymous')

            ewm.src = ewmimg;
        };
        img.src = data.bgimg;
    }
    function qr(link) {
        $("#qrPic").empty();
        var str = toUtf8(link);
        $("#qrPic").qrcode({
            render: "canvas",
            width: 400,
            height: 400,
            text: str
        });

        $("canvas").attr("id", "erw");
        var canvas = document.getElementById('erw');
        var context = canvas.getContext('2d');
        var strDataURI = canvas.toDataURL();
        return strDataURI;
    }
    function toUtf8(str) {
        var out, i, len, c;
        out = "";
        len = str.length;
        for (i = 0; i < len; i++) {
            c = str.charCodeAt(i);
            if ((c >= 0x0001) && (c <= 0x007F)) {
                out += str.charAt(i);
            } else if (c > 0x07FF) {
                out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));
                out += String.fromCharCode(0x80 | ((c >> 6) & 0x3F));
                out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
            } else {
                out += String.fromCcodeharCode(0xC0 | ((c >> 6) & 0x1F));
                out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
            }
        }
        return out;
    }

    function draw_long_text(text,cxt,left,top,maxWidth){
        var lineindex = 1;
        var offsetleft = left;
        var offsettop = top;
        var strlength = text.length;
        for(var i = 0;i<strlength;i++){
            if(offsetleft+30 < maxWidth){
                cxt.fillText(text[i], offsetleft, offsettop);
                offsetleft += 30;
            }else{
                offsetleft = 30;
                offsettop += 35;
                maxWidth += maxWidth;
                cxt.fillText(text[i], offsetleft, offsettop);
            }
            console.log(text[i],'::',offsetleft,',',offsettop,',',maxWidth);

        }
    }

    function findBreakPoint (text, width, context) {
        var min = 0;
        var max = text.length - 1;
        while (min <= max) {
            var middle = Math.floor((min + max) / 2);
            var middleWidth = context.measureText(text.substr(0, middle)).width;
            var oneCharWiderThanMiddleWidth = context.measureText(text.substr(0, middle + 1)).width;
            if (middleWidth <= width && oneCharWiderThanMiddleWidth > width) {
                return middle;
            }
            if (middleWidth < width) {
                min = middle + 1;
            } else {
                max = middle - 1;
            }
        }

        return -1;
    }
    function breakLinesForCanvas (text, width, font) {
        var canvas = document.createElement('canvas');
        canvas.width = width;
        canvas.height = width;
        var context = canvas.getContext('2d');
        var result = [];
        var breakPoint = 0;
        if (font) {
            context.font = font;
        }
        while ((breakPoint = findBreakPoint(text, width, context)) !== -1) {
            result.push(text.substr(0, breakPoint));
            text = text.substr(breakPoint);
        }

        if (text) {
            result.push(text);
        }

        return result;
    }

</script>

</body>
</html>